package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rainng.coursesystem.dao.mapper.BuildingMapper;
import com.rainng.coursesystem.model.entity.BuildingEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BuildingDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;

    private final BuildingMapper mapper;

    public BuildingDAO(BuildingMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(BuildingEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public BuildingEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(BuildingEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String name) {
        LambdaQueryWrapper<BuildingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, BuildingEntity::getName, name);

        return mapper.selectCount(wrapper);
    }

    public List<BuildingEntity> getPage(Integer index, String name) {
        LambdaQueryWrapper<BuildingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, BuildingEntity::getName, name);

        return selectPage(mapper, wrapper, index, PAGE_SIZE);
    }

    public List<BuildingEntity> listName() {
        LambdaQueryWrapper<BuildingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BuildingEntity::getId, BuildingEntity::getName,BuildingEntity::getRemarks);
        return mapper.selectList(wrapper);
    }
}
